<?php
namespace app\admin\controller;
use \app\admin\model\Agent as AgentModel;
use app\lib\exception\SuccessMessage;
use think\exception\ErrorException;
use app\lib\exception\LoginException;
use think\Request;
use app\lib\validate\AgentAddCheck;
use app\admin\model\AgentAuthorize;
use app\admin\model\AgentCard;



class Agent extends Base{
    public function index() {
        $list = getAgentList();
        $this->assign('list', $list);
        return view();
    }

   

    //编辑
    public function edit() {
        $agent_id = Request::instance()->param('id');
        $list = getAgentDetail($agent_id);
        $this->assign('list', $list);
        return view();
    }
    //查看费率
    public function showFee()
    {
        $chnlId = Request::instance()->param('id');
        $list = getFee($chnlId);
        foreach($list as $k=>&$v){
            $v['t0Fee'] = (substr($v['t0Fee'],4,6)*100).'%';
        }
        $this->assign('list', $list);
        return view();
    }
    //商户列表
    public function users()
    {
        //$chnlId = Request::instance()->param('id');
        $page =  input('page') ? input('page') : 1;
        $arr['reqType'] = 'merList';
        $arr['agentId'] =  config('api.agentId');
        $arr['chnlId'] = '';
        $arr['merId'] = '';
        $arr['merMp'] = '';
        $arr['certNo'] = '';
        $arr['merName'] = '';
        $arr['merStat'] = '';
        $arr['beginRegDate'] = 20170901;
        $arr['endRegDate'] = date('Ymd');
        $arr['pageNum'] = $page;
        $arr['pageSize'] = '10';
        $url = config('api.reqUrl').'merEntry.do';
        $passStr = config('api.passStr');
        $str = $arr['reqType'].$arr['agentId'].$arr['chnlId'].$arr['merId'].$arr['merMp'].$arr['certNo'].$arr['merName']
            .$arr['merStat'].$arr['beginRegDate'].$arr['endRegDate'].$arr['pageNum'].$arr['pageSize'].$passStr ;
        $arr['chkValue'] = md5($str);
        $resdata = curl_form($url,$arr);
        $resdata = urldecode($resdata);
        $list = json_decode(mb_convert_encoding($resdata,'UTF-8','GBK'),true);
        if($list['mersInfo'] && is_array($list['mersInfo']))
        {
            foreach($list['mersInfo'] as &$value){
                $value['merStat'] = $value['merStat'] == 'S' ? '已实名' : '未实名' ;
            }
        }
        $this->assign('page', $list['pageNum']);
        $this->assign('totalPage', $list['totalPage']);
        $this->assign('list', $list['mersInfo']);
        return view();

    }
    
    public function agent()
    {
        $list = getAgentList();
        if($list && is_array($list) )
        {
            foreach($list as $key=>$value){
                $list[$key]['name'] = $list[$key]['chnlName'];
            }
            $tree = arrToTree($list);
        }

        throw new SuccessMessage([
            'data' =>   $tree
        ]);   
    }
   
    //代理商结构图
    public function construct() {
        
        return view();
    }

    public function checkAgentAdd() {
        (new AgentAddCheck)->gocheck();
        $data = Request::instance()->post();
        if(!isset($data['edit_id'])) {
            if(count((new AgentModel)->where(array('mobile' => $data['mobile']))->find()) != 0) {
                throw new LoginException([
                    'msg' => '该手机号的用户已注册'
                ]);
            }
        }
        return $data;
    }

    //递归生成树
    function FormatTree($data, $pid) {
        $arr = array();
        $tem = array();
        foreach ($data as $v) {
            if ($v['uper_id'] == $pid) {
                $arr['id'] = $v['id'];
                $arr['name'] = $v['name'];
                $arr['pid'] = $v['uper_id'];
                $arr['childrens'] = AgentModel::where(array('uper_id' => $v['id']))->select();

                $tem = $this->FormatTree($data, $v['id']);//递归，调用自身
            }
        }
        return $arr;
    }
}